environment *add_env(environment *env, identifier *id) {
  //ast.c L61 :: NEW_IDENTIFIER(n) = PyString_InternFromString(STR(n))
  //node.h L29 :: STR(N) = (n)->n_str (char *str)
  //pythonvoid.c L100:: PyString_InternFromString (calls PyString_FromStringAndSize)
  //pythonvoid.c L72 :: PyString_FromStringAndSize (returns PyObject of type PyString)
  //pythonvoid.c L28 :: PyString_AsString (returns char part of PyString object)
  char *name = PyString_AsString(id);

  //make new environment element
  environment *new;
  new = (environment *)malloc(sizeof(environment));

  //point to previous element
  current->prev = env;
  
  //get the tree pointer corresponding to the string name
  current->id = lookup(name);
  
  return current;
}


//builds an arraylist with trees as data from an environment linked list.
arraylist *convert_env(environment *env) {
  environment *current = env;
  int size = 0;
  int i;
  while(current) {
    size++;
    current = current->prev;
  }
  print_env(env);
  sort_env(current);
  print_env(env);
  arraylist *l = arraylist_create(size);
  while (env) {
    arraylist_insert(l,env->id);
    env = env->prev;
  } 
  arraylist_print(l);
  return l;
}


void print_env(environment *env) {
  while (env) {
    printf("%p, ",env->id);
    env = env->prev;
  }
}

/*
sorts an environmnent linkedlist.. 
since the input list has links to the previous elements
we actually sort the list backwards..(I think).. Arraylist conversion
should handle this though. 
*/
void sort_env(environment *env) {
  environment *a = NULL;
  environment *b = NULL;
  environment *c = NULL;
  environment *e = NULL;
  environment *tmp = NULL;

  environment *head = env;

  //bubblesort - yikes :P   
  while(e != head->prev) {  
    c = a = head;
    b = a->prev;
    while(a != e) {
      if(a->id > b->id) {
	if(a == head) {
	  tmp = b->prev;
	  b->prev = a;
	  a->prev = tmp;
	  head = b;
	  c = b;
	} else {
	  tmp = b->prev;
	  b->prev = a;
	  a->prev = tmp;
	  c->prev = b;
	  c = b;
	}
      } else {
	c = a;
	a = a->prev;
      }
      b = a->prev;
      if(b == e)
	e = a;
    }
  }
}



arraylist *arraylist_create() {

}

//return item at index
void *arraylist_get(arraylist *l, int index) {
  
}

//return index of item
int arraylist_search(arraylist *l, void *needle) {
  
}

//insert item in the end
void arraylist_insert(arraylist *l, void *obj) {

}
